<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>编辑文章</title>
    <style>
        .content{
            width: 600px;
            height: 200px;
        }
        .error{
            font-size: 12px;
            color: red;
        }
    </style>
    <script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.min.js"></script>
    <script>

        function check(){

            //初始化，清空报错信息
            $("#err_blog_title").html('');
            $("#err_blog_content").html('');

            //验证发布文章的数据

            if ($("#blog_title").val() == "") {
                $("#err_blog_title").html('提示：请填写文章标题！');
                $("#blog_title").focus();
                return false;
            }

            if ($("#blog_content").val() == "") {
                $("#err_blog_content").html('提示：请填写文章内容！');
                $("#blog_content").focus();
                return false;
            }


        }

    </script>
    <meta name="keywords" content="个人博客" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="css/index.css"/>
    <link rel="stylesheet" href="css/style.css"/>
    <script type="text/javascript" src="js/jquery1.42.min.js"></script>
    <script type="text/javascript" src="js/jquery.SuperSlide.2.1.1.js"></script>
    <!--[if lt IE 9]>
    <script src="js/html5.js"></script>
    <![endif]-->
</head>
<body>

>
<!--nav-->
<div id="nav">
    <ul>
        <li><a href="list.php">文章列表</a></li>
        <li><a href="add_blog.php">发布文章</a></li>
        <li><a href="changePwd.php">修改密码</a></li>
        <div class="clear"></div>
    </ul>
</div>
<!--nav end-->
<!--content start-->
<div id="content">
    <!--left-->
    <div class="left" id="guestbook">
        <div class="weizi">
            <div class="wz_text">当前位置：<a href="#">首页</a>><h1>编辑文章</h1></div>
        </div>
        <div class="g_content">
            <?php

            //连接数据库
            include 'inc/conn.php';

            //验证用户登陆
            include 'inc/checkSesion.php';

            //加载页面头部
            include 'inc/admin_header.php';

            $id = intval($_GET['id']);  //当前要编辑的博文的ID
            $sql = "SELECT blog_title, blog_content FROM blog_info WHERE id=$id ";
            $res = mysqli_query($conn, $sql);
            if (!($rwt = mysqli_fetch_array($res))) {
                die("<script>history.back();</script>");
            }


            if ($_GET['act'] === 'editing') {

                //编辑文章的操作

                $blog_title = $_POST['blog_title'];
                $blog_content = $_POST['blog_content'];

                if (empty($blog_title) || empty($blog_content)) {
                    die("<script>alert('提示：内容有误！');history.back();</script>");
                }

                $sqlEdit = "UPDATE blog_info SET blog_title='".htmlspecialchars($blog_title, ENT_QUOTES)."' ,
                blog_content='".htmlspecialchars($blog_content, ENT_QUOTES)."',
                 up_time=".time()." 
                 WHERE id=".$id;

                $resAdd = mysqli_query($conn, $sqlEdit);


                if (!$resAdd) {
                    die("<script>alert('提示：内容有误！');history.back();</script>");
                }

                echo "<script>alert('文章修改成功！'); window.location.href='list.php';</script>";

            }

            ?>

            <div>

                <form id="form" action="?act=editing&id=<?=$id?>" method="post" onsubmit="return check();">

                    <p>
                        文章标题：<input type="text" name="blog_title" id="blog_title" value="<?=$rwt['blog_title']?>">
                        <label id="err_blog_title" class="error"></label>
                    </p>

                    <p>
                        文章内容：
                        <textarea name="blog_content" id="blog_content" class="content"><?=$rwt['blog_content']?></textarea>
                        <label id="err_blog_content" class="error"></label>
                    </p>

                    <p>
                        <button>发布文章</button>
                        <button type="button" onclick="window.location.href='list.php';">取消发布</button>
                    </p>

                </form>

            </div>
        </div>
    </div>


<!--footer end-->
<script type="text/javascript">jQuery(".lanmubox").slide({easing:"easeOutBounce",delayTime:400});</script>
<script  type="text/javascript" src="js/nav.js"></script>


</body>
</html>